package com.qf.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.entity.Dept;
import com.qf.entity.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/j")
public class JsonController {

    /**
     * 测试 部门对象 转换为json
     * @return
     */
    @RequestMapping("/test2")
    public String testJson02() throws ParseException, JsonProcessingException {
        Dept dept = new Dept();
        dept.setId(10);
        dept.setName("开发部");

        ArrayList<User> users = new ArrayList<>();

        User user = new User();
        user.setId(100);
        user.setName("zhanghe");
        user.setGender(true);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse("1990-01-01");
        user.setBirth(date);

        users.add(user);

        User user1 = new User();
        user1.setId(200);
        user1.setName("lisi");
        user1.setGender(true);
        Date date1 = simpleDateFormat.parse("1996-01-01");
        user1.setBirth(date1);

        users.add(user1);

        dept.setUsers(users);

        // 把部门对象转换为 json
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(dept);
        System.out.println(json);

        // 把json转换为java对象
        Dept dept1 = objectMapper.readValue(json, Dept.class);
        System.out.println(dept1);

        return json;

    }


    /**
     * 测试自己写代码进行json和java对象之间的转换
     * @return
     */
    @RequestMapping("/test1")
    public String testJson() throws ParseException, JsonProcessingException {
        User user = new User();
        user.setId(100);
        user.setName("zhanghe");
        user.setGender(true);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse("1990-01-01");
        user.setBirth(date);
        // user对象转换为 json字符串
        ObjectMapper objectMapper = new ObjectMapper();  // json转换器对象
        String json = objectMapper.writeValueAsString(user);
        System.out.println(json);
        // json字符串转换为user对象
        User user1 = objectMapper.readValue(json, User.class);
        System.out.println(user1);
        return json;
    }


    /**
     * {
     * 	"code":"1",
     * 	"msg":"请求成功",
     * 	"data":[
     *                        {
     * 			id: 100,
     * 			name: "zhanghe",
     * 			gender: true,
     * 			birth: "1990-01-01"
     *            },
     *            {
     * 			id: 200,
     * 			name: "lisi",
     * 			gender: true,
     * 			birth: "1996-01-01"
     *            }
     * 		]
     * }
     * @return
     * @throws ParseException
     */
    @RequestMapping("/all")
    public HashMap<String,Object> getusers() throws ParseException {
        HashMap<String, Object> hashMap = new HashMap<>();
        ArrayList<User> users = new ArrayList<>();

        User user = new User();
        user.setId(100);
        user.setName("zhanghe");
        user.setGender(true);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse("1990-01-01");
        user.setBirth(date);

        users.add(user);

        User user1 = new User();
        user1.setId(200);
        user1.setName("lisi");
        user1.setGender(true);
        Date date1 = simpleDateFormat.parse("1996-01-01");
        user1.setBirth(date1);

        users.add(user1);

        hashMap.put("code","1");
        hashMap.put("msg","ok");
        hashMap.put("data",users);
        return hashMap;
    }

    @RequestMapping("/getall")
    public List<User> getAll() throws ParseException {
        ArrayList<User> users = new ArrayList<>();

        User user = new User();
        user.setId(100);
        user.setName("zhanghe");
        user.setGender(true);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse("1990-01-01");
        user.setBirth(date);

        users.add(user);

        User user1 = new User();
        user1.setId(200);
        user1.setName("lisi");
        user1.setGender(true);
        Date date1 = simpleDateFormat.parse("1996-01-01");
        user1.setBirth(date1);

        users.add(user1);

        return users;
    }



    @RequestMapping("/get")
    public User getuser(Integer id) throws ParseException {
        User user = new User();
        user.setId(id);
        user.setName("zhanghe");
        user.setGender(true);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse("1990-01-01");
        user.setBirth(date);
        return user;
    }

}
